<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item>
          <el-input v-model="form.merchant_id" placeholder="商户ID" />
        </el-form-item>
        <el-form-item>
          <el-input v-model="form.merchant_name" placeholder="商户名称" />
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.coin_id" placeholder="币种" style="width:120px;">
            <el-option
              v-for="item in coinsList"
              :key="item.coin_id"
              :label="item.coin_name"
              :value="item.coin_id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="form.trade_type" placeholder="交易类型">
            <el-option
              v-for="item in tradesList"
              :key="item.trade_type"
              :label="item.trade_name"
              :value="item.trade_type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <!--<el-button type="primary" plain @click="onExport">导出</el-button>-->
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="merchant_id" label="商户ID" align="center"/>
          <el-table-column prop="merchant_name" label="商户名称" align="center" width="180" />
          <el-table-column prop="trade_type" label="交易类型" align="center" width="150"/>
          <el-table-column prop="coin_name" label="币种名称" align="center"/>
          <el-table-column prop="amount" label="交易数量" align="center">
            <template #default="scope">
                {{scope.row.sg}} {{scope.row.amount}}
            </template>
          </el-table-column>
          <el-table-column prop="amount_before" label="交易前数量" align="center"/>
          <el-table-column prop="amount_after" label="交易后数量" align="center" />
          <el-table-column prop="freeze_before" label="冻结前数量"  align="center" />
          <el-table-column prop="freeze_after" label="冻结后数量" align="center" />
          <el-table-column prop="record_id" label="关联ID" align="center" />
          <el-table-column prop="description" label="备注" align="center" />
          <el-table-column prop="createtime" label="交易时间" align="center" :formatter="formatterTime" width="180"/>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive, computed } from "vue";
import { formatterTime, timestamp, pickerDefaultTime } from "@/utils/utils";
import { marginRecordApi } from "@/api/payServerse/wallet";
import { GlobalStore } from "@/store";
const globalStore = GlobalStore();
const loading = ref(false);
const coinsList = computed(() => globalStore.coins);
const tradesList = computed(() => globalStore.trades);
const tableData = ref([]);

const formData = {
  merchant_id: "",
  merchant_name: "",
  coin_id: "",
  trade_type: "",
  timeData: [],
};

const form = reactive({ ...formData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    merchant_id: form.merchant_id,
    merchant_name: form.merchant_name,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    coin_id: form.coin_id,
    trade_type: form.trade_type,
  };
};

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;
  try {
    const res = await marginRecordApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
